package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwbdT;
import cn.e4j.modular.crm.entity.GwbdlT;
import cn.e4j.modular.crm.mapper.GwbdTMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 职位信息 服务实现类
 *
 * @author liuw
 * @date 2019/09/04
 */
@Service
public class Gwat233ServiceImpl extends ServiceImpl<GwbdTMapper, GwbdT>{

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getListData(Page<Object> page, Dto dto) {
        List<Map<String, Object>> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(GwbdT gwbdT) {
        QueryWrapper<GwbdT> gwbdTQueryWrapper = new QueryWrapper<>();
        gwbdTQueryWrapper.eq("GWBDENT",gwbdT.getGWBDENT());
        gwbdTQueryWrapper.eq("GWBD001",gwbdT.getGWBD001());
        return this.baseMapper.selectList(gwbdTQueryWrapper).size() > 0;
    }

    @Transactional
    public void addData(GwbdT gwbdT, GwbdlT gwbdlT){
        gwbdT.insert();
        gwbdlT.insert();
    }

    @Transactional
    public void editData(GwbdT gwbdT, GwbdlT gwbdlT,GwbdT oldGwbdT){
        //修改1
        UpdateWrapper<GwbdT> gwbdTUpdateWrapper = new UpdateWrapper<>();
        gwbdTUpdateWrapper.eq("GWBDENT",oldGwbdT.getGWBDENT());
        gwbdTUpdateWrapper.eq("GWBD001",oldGwbdT.getGWBD001());
        //修改2
        UpdateWrapper<GwbdlT> gwbdlTUpdateWrapper = new UpdateWrapper<>();
        gwbdlTUpdateWrapper.eq("GWBDLENT",oldGwbdT.getGWBDENT());
        gwbdlTUpdateWrapper.eq("GWBDL001",oldGwbdT.getGWBD001());
        gwbdlTUpdateWrapper.eq("GWBDL002",gwbdlT.getGWBDL002());

        gwbdT.update(gwbdTUpdateWrapper);
        gwbdlT.update(gwbdlTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GwbdT gwbdT, GwbdlT gwbdlT){
        delete(gwbdT,gwbdlT);
    }

    @Transactional
    public void deleteDataBatch(List<GwbdT> gwbdTList,List<GwbdlT> gwbdlTList){
        for(int i=0; i < gwbdTList.size(); i++){
            delete(gwbdTList.get(i),gwbdlTList.get(i));
        }
    }

    private void delete(GwbdT gwbdT, GwbdlT gwbdlT){
        //修改1
        UpdateWrapper<GwbdT> gwbdTUpdateWrapper = new UpdateWrapper<>();
        gwbdTUpdateWrapper.eq("GWBDENT",gwbdT.getGWBDENT());
        gwbdTUpdateWrapper.eq("GWBD001",gwbdT.getGWBD001());
        //修改2
        UpdateWrapper<GwbdlT> gwbdlTUpdateWrapper = new UpdateWrapper<>();
        gwbdlTUpdateWrapper.eq("GWBDLENT",gwbdlT.getGWBDLENT());
        gwbdlTUpdateWrapper.eq("GWBDL001",gwbdlT.getGWBDL001());
        gwbdlTUpdateWrapper.eq("GWBDL002",gwbdlT.getGWBDL002());

        gwbdT.delete(gwbdTUpdateWrapper);
        gwbdlT.delete(gwbdlTUpdateWrapper);
    }


}
